package com.hyf.meterialdesign.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.hyf.meterialdesign.R;
import com.hyf.meterialdesign.adapter.SimpleFragmentAdaptper;
import com.hyf.meterialdesign.base.BaseAppBarActivity;
import com.hyf.meterialdesign.fragment.SimpleFragment;
import com.wyt.searchbox.SearchFragment;
import com.wyt.searchbox.custom.IOnSearchClickListener;

import java.util.ArrayList;
import java.util.List;

public class BaseCollaspingToolbarActivity extends BaseAppBarActivity {

    private CoordinatorLayout rootView;

    private Toolbar toolbar;

    private CollapsingToolbarLayout collapsingLayout;

    private AppBarLayout appbar;

    private ViewPager vpHeader;
    private SearchFragment searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_base_collasping_toolbar);
        initView();initData();initListener();
    }

    private void initListener() {
        searchView.setOnSearchClickListener(new IOnSearchClickListener() {
            @Override
            public void OnSearchClick(String keyword) {
                if (TextUtils.isEmpty(keyword))return;
                Toast.makeText(BaseCollaspingToolbarActivity.this, keyword, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        rootView = (CoordinatorLayout) findViewById(R.id.activity_base_collasping_toolbar);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        collapsingLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingLayout);
        appbar = (AppBarLayout) findViewById(R.id.appbar);
        vpHeader = (ViewPager) findViewById(R.id.vpHeader);
    }

    private void initData() {
//        toolbar.setTitle("title");
//        toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT)
        {
            ViewGroup.LayoutParams params = toolbar.getLayoutParams();
            params.height = params.height+getStatusBarHeight();
            toolbar.setLayoutParams(params);
            toolbar.setPadding(0,getStatusBarHeight(),0,0);
        }
        toolbar.inflateMenu(R.menu.menu_base_collasping_toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.search){
                    searchView.show(getSupportFragmentManager(),SearchFragment.TAG);
                }
                return true;
            }
        });

        initAppBarLayout();
        initViewPager();
        initSearchView();
    }

    private void initSearchView() {
        searchView = SearchFragment.newInstance();
    }

    private void initViewPager() {
        List<SimpleFragment> headerFragments = new ArrayList<>();
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491554730265&di=6134e601b351ff254e77e4096412979d&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201508%2F08%2F20150808112252_MUhiH.jpeg",false));
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491554784000&di=5ce5441c25325032072b47b53cac051f&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201505%2F10%2F20150510001633_TmsQR.thumb.700_0.jpeg",false));
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491554865529&di=4467b85ada68155e4f193595a7448a3f&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201605%2F08%2F20160508181552_WeKRX.jpeg",false));
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491555017920&di=508ea534773780aef84bc36a59bc62b4&imgtype=0&src=http%3A%2F%2Fm.qqzhi.com%2Fupload%2Fimg_0_1342580691D3780971425_23.jpg",false));
        vpHeader.setAdapter(new SimpleFragmentAdaptper(getSupportFragmentManager(),headerFragments));
    }

    private void initAppBarLayout() {
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//                if (verticalOffset==0){
//                    toolbar.setVisibility(View.INVISIBLE);
//                    return;
//                }
//                toolbar.setVisibility(View.VISIBLE);
                float alpha = (float) (-verticalOffset)/appBarLayout.getTotalScrollRange();
                if (toolbar.getChildAt(0) instanceof TextView) {
                    toolbar.getChildAt(0).setAlpha(alpha);
                }
            }
        });
    }

    public void showSnack(View view) {
        Snackbar snackbar = Snackbar.make(rootView, "啦啦啦啦啦德玛西亚", Snackbar.LENGTH_LONG);
        snackbar.show();
    }

    public void showToast(View view) {
        Toast.makeText(this, "啦啦啦啦啦德玛西亚", Toast.LENGTH_SHORT).show();
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
